import json
import time
import tornado.web
from newpro.util.myjwt import myjwt
from newpro.util.myredis import r


class BaseHandler(tornado.web.RequestHandler):
    # 重写该方法
    def set_default_headers(self):
        self.set_header("Access-Control-Allow-Origin", "*")  # 允许所有的域名访问
        self.set_header("Access-Control-Allow-Headers", "*")  # 允许携带所有的参数
        self.set_header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE")  # 允许所有的请求方式
        self.set_header("Access-Control-Max-Age", "3600")  # 允许连接时，最大响应时间
    # 定义一个响应的方法，不需要实现什么功能
    def options(self):
        pass

    # def pomilist(self):
    #     uri = self.request.uri
    #     whilelist = ["/login"]
    #     if uri not in whilelist:
    #         headers = self.request.headers
    #         try:
    #             token = headers['token']
    #             flag = myjwt.jwt_check(token)
    #             if flag == True:
    #                 payload = myjwt.jwt_de(token)
    #                 tim = payload['data']['exp']
    #                 now = time.time()
    #                 if not(now)-int(tim) > 36:
    #                     self.finish({'code': 402})
    #
    #                 key = "logoutlist"
    #                 res = r.sortedset_getall(key)
    #                 for i in res:
    #                     if token == i.decode():
    #                         self.finish({'code': 409})
    #                 key = "interlist" + str(payload['data']['roleid'])
    #                 interlist = json.loads(r.get_str(key))
    #                 if uri not in interlist:
    #                     self.finish({'code': 408})
    #             else:
    #                 self.finish({'code': 403})
    #         except:
    #             self.finish({'code': 401})

